package com.lovo.web.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovo.commom.remoteMethod.ISysProductRemote;
import com.lovo.web.bean.EsBean;
import com.lovo.web.bean.ProductBean;
import com.lovo.web.bean.ProductInfoBean;
import com.lovo.web.bean.ProductListingReviewBean;
import com.lovo.web.mapper.IEsMapper;
import com.lovo.web.mapper.IProductInfoMapper;
import com.lovo.web.mapper.IProductListingReviewMapper;
import com.lovo.web.mapper.IProductMapper;
import com.lovo.web.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {
    @Autowired
    private IProductMapper productMapper;
    @Autowired
    private ISysProductRemote sysProductRemote;
    @Autowired
    private IProductInfoMapper infoMapper;
    @Autowired
    ISysProductRemote productRemote;
    @Autowired
    IProductInfoMapper productInfoMapper;
    @Autowired
    IEsMapper esMapper;
    @Autowired
    IProductListingReviewMapper productListingReviewMapper;

    @Override
    public IPage<ProductBean> findProductTypeLst(String type, int pageNO, int isPromotion) {
        return productMapper.findProductTypeLst(new Page<>(pageNO, 20), type, isPromotion);
    }

    @Override
    public List<ProductBean> findTypeLst() {
        return productMapper.findTypeLst();
    }

    @Override
    public IPage<ProductBean> daishangjiaProduct(Integer pageNO) {
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        qw.ne("p_status", 3);
        return productMapper.selectPage(new Page<>(pageNO, 10), qw);
    }

    @Override
    public ProductBean findByName(String name) {
        return productMapper.findByName(name);
    }

    @Override
    public void updateProductStatus(String productName, int status) {
        QueryWrapper<ProductBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p_productName", productName);
        ProductBean product = productMapper.selectOne(queryWrapper);
        product.setStatus(status);
        productMapper.updateById(product);
    }

    @Override
    public void updateProductStatusSelf(ProductBean productBean) {
        //修改商品状态为1--未审核
        QueryWrapper<ProductBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("p_productName", productBean.getProductName());
        ProductBean product = productMapper.selectOne(queryWrapper);
        product.setStatus(1);
        productMapper.updateById(product);
        //修改明细的销售价
        for (ProductInfoBean pi : productBean.getProductInfoList()) {
            infoMapper.updateById(pi);
        }
        //远程修改明细的销售价,远程修改商品状态为1--待上架
        String str = JSON.toJSONString(productBean);
        boolean b = productRemote.updateProductInfoPrice(str);
        if (!b) {
            throw new RuntimeException();
        }
    }

    //每天0点从后台数据库拉取待上架商品保存到商品表和商品详情表
    @Scheduled(cron = "0 1-59 * * * ? ")
    @Override
    public void findByProductStatus() {
        //得到待上架商品集合数据
        String listObj = sysProductRemote.findByProductStatus();
        List<ProductBean> productBeanList = JSONObject.parseArray(listObj, ProductBean.class);
        for (ProductBean productBean : productBeanList) {
            productBean.setStatus(0);
            productBean.setRate(1);
            //添加商品到商品表
            productMapper.insert(productBean);
            //取出商品详情集合
            List<ProductInfoBean> infoList = productBean.getProductInfoList();
            for (ProductInfoBean infoBean : infoList) {
                infoBean.setProductName(productBean.getProductName());
                //添加详情到详情表
                infoMapper.insert(infoBean);
            }
        }
    }

    @Override
    public void inku(String productName, String specs, int num) {
        //修改product表
        QueryWrapper<ProductBean> qw = new QueryWrapper<>();
        qw.eq("p_productName", productName);
        ProductBean product = productMapper.selectOne(qw);
        product.setNum(product.getNum() + num);
        productMapper.updateById(product);
        //修改库存明细
        QueryWrapper<ProductInfoBean> qw2 = new QueryWrapper<>();
        qw2.eq("pi_specs", specs);
        //商品名
        qw2.eq("pi_productName", productName);
        ProductInfoBean productInfo = productInfoMapper.selectOne(qw2);
        productInfo.setInventory(productInfo.getInventory() + num);
        productInfoMapper.updateById(productInfo);
    }

    @Override
    public IPage<ProductBean> findByKeyWord(int pageNO, String keyWord) {
        //通过es查询关键字，得到es分页对象
        PageRequest pageRequest = PageRequest.of(pageNO - 1, 8);
        org.springframework.data.domain.Page<EsBean> page = esMapper.findByInfo(keyWord, pageRequest);
        //es分页对象流操作处理得到雪花ID
        List<String> snowIdList = page.stream().map(EsBean::getNewsId).toList();
        //通过得到的雪花ID集合查询数据库得到商品集合
        List<ProductBean> productBeanList = productMapper.findBySnowId(snowIdList);
        //将得到的商品集合放入分页对象中
        IPage<ProductBean> productBeanIPage = new Page<>();
        productBeanIPage.setRecords(productBeanList);
        productBeanIPage.setTotal(productBeanList.size());
        productBeanIPage.setSize(page.getSize());
        productBeanIPage.setCurrent(pageNO);
        return productBeanIPage;
    }

    @Override
    public void updateIsPermission(List<String> productNames, float discount) {
        productMapper.updateIsPromotion(productNames, discount);
    }

    @Override
    public ProductListingReviewBean findPLRByName(String name) {
        QueryWrapper<ProductListingReviewBean> qw = new QueryWrapper<>();
        qw.eq("pl_productName",name).orderByDesc("pk_reviewId").last("limit 1");
        ProductListingReviewBean bean = productListingReviewMapper.selectOne(qw);
        return bean;
    }
}
